package com.tanhua.management.service;

import cn.hutool.core.collection.CollUtil;


import cn.hutool.core.convert.Convert;
import com.tanhua.management.api.CustomerApi;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.management.vo.ClientVo;
import com.tanhua.management.vo.CustomerVo;
import com.tanhua.management.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ManageService {

    @Autowired
    private CustomerApi customerApi;


    public PageResult selectList(Integer page, Integer pageSize, String id, String nickname, String city) {
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);


        PageInfo<CustomerVo> customersList=new PageInfo<>();
        if (null!=id&&null==nickname&&null==city){
            customersList= this.customerApi.findById(page,pageSize,id);


        }else if(null==id&&null==nickname&&null==city){
            customersList= this.customerApi.findall(page,pageSize);
        }else if (null==id&&null!=nickname&&null==city){
            customersList= this.customerApi.findByNickname(page,pageSize,nickname);
        }else if (null==id&&null==nickname&&null!=city) {
            customersList = this.customerApi.findByCity(page, pageSize, city);
        }else if (null!=id&&null!=nickname&&null==city) {
            customersList = this.customerApi.findByIdNickname(page, pageSize, id,nickname);
        }
        Integer total = customersList.getTotal();
        pageResult.setCounts(total);

        pageResult.setPages(Convert.toInt(Math.ceil(Convert.toDouble(total)/pageSize)));

        if (CollUtil.isEmpty(customersList.getRecords())){
            return pageResult;
        }

        pageResult.setItems(customersList.getRecords());

        return pageResult;
    }

    public ClientVo selectByuserID(String userId) {
        if (null!=userId){
            ClientVo clientVo = this.customerApi.findClientVo(userId);

            return clientVo;
        }
        return null;
    }


    public boolean insertfreeze(String userId, Integer freezingTime, Integer freezingRange, String reasonsForFreezing, String frozenRemarks) {


        return false;
    }
}
